package com.ljx.NoteBody.service.impl;

import com.ljx.NoteBody.common.MyException;
import com.ljx.NoteBody.dto.NoteDto;
import com.ljx.NoteBody.entity.Label;
import com.ljx.NoteBody.entity.Note;
import com.ljx.NoteBody.entity.User;
import com.ljx.NoteBody.mapper.LabelMapper;
import com.ljx.NoteBody.mapper.NoteMapper;
import com.ljx.NoteBody.mapper.UserMapper;
import com.ljx.NoteBody.service.NoteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    NoteMapper noteMapper;

    @Autowired
    LabelMapper labelMapper;

    @Autowired
    UserMapper userMapper;

    @Transactional
    @Override
    public void saveNote(NoteDto noteDto,  HttpServletRequest httpServletRequest) {
        LocalDateTime time = LocalDateTime.now();
        // 保存笔记
        noteDto.setCreateTime(time);
        noteDto.setUpdateTime(time);
        int i = noteMapper.saveNote(noteDto);
        if(i <= 0) throw new MyException("笔记保存错误");
        // 保存标签
        Label label = new Label();
        label.setCreateTime(time);
        label.setUpdateTime(time);
        label.setNoteId(noteDto.getId());
        for(String s: noteDto.getNoteLabels()){
            label.setContent(s);
            i = labelMapper.saveLabel(label);
            if(i <= 0) throw new MyException("标签保存错误");
        }
    }

    @Override
    public Map<String, Object> newNotes(int page, int pageSize, String username) {
        Map<String, Object> map = new HashMap<>();
        List<NoteDto> data = new ArrayList<>();
        User queryUser = new User();
        for(Note note: noteMapper.selectNewNotes(page, pageSize, username)){
            NoteDto noteDto = new NoteDto();
            // 对象融合
            BeanUtils.copyProperties(note, noteDto);
            // 设置用户信息
            queryUser = userMapper.queryUserByUsername(note.getUsername());
            noteDto.setNickname(queryUser.getNickname());
            noteDto.setAvatar(queryUser.getAvatar());
            // 设置标签
            noteDto.setNoteLabels(labelMapper.queryLabelByNoteId(note.getId()));
            data.add(noteDto);
        }
        map.put("newNotes", data);
        map.put("totalPage", noteMapper.totalPage(username));
        return map;
    }

    @Override
    public NoteDto selectNoteById(Long id) {
        int i = noteMapper.addNoteViewsCount(id);
        if(i <= 0) throw new MyException("添加笔记浏览量出错");
        // 设置笔记信息
        NoteDto noteDto = new NoteDto();
        Note note = noteMapper.selectNoteById(id);
        User user = userMapper.queryUserByUsername(note.getUsername());
        BeanUtils.copyProperties(note, noteDto);
        // 设置用户信息
        noteDto.setNickname(user.getNickname());
        noteDto.setAvatar(user.getAvatar());
        // 设置标签
        noteDto.setNoteLabels(labelMapper.queryLabelByNoteId(note.getId()));

        return noteDto;
    }

    @Transactional
    @Override
    public void updateNote(NoteDto noteDto, HttpServletRequest httpServletRequest) {
        LocalDateTime time = LocalDateTime.now();
        // 保存笔记
        noteDto.setUpdateTime(time);
        int i = noteMapper.updateNote(noteDto);
        if(i <= 0) throw new MyException("笔记保存错误");
        // 保存标签
        Label label = new Label();
        label.setUpdateTime(time);
        label.setCreateTime(time);
        label.setNoteId(noteDto.getId());
        labelMapper.deleteNote(noteDto.getId());
        for(String s: noteDto.getNoteLabels()){
            label.setContent(s);
            i = labelMapper.saveLabel(label);
            if(i <= 0) throw new MyException("标签保存错误");
        }
    }

    @Override
    public List<Note> queryHotNotes() {
        return noteMapper.queryHotNotes();
    }

    @Override
    public void deleteNote(Long noteId) {
        int i = noteMapper.deleteNote(noteId);
        if(i <= 0) throw new MyException("删除笔记错误");
    }
}
